{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 随机数生成\n",
    "> numpy.random模块对Python内置的random进行了补充，增加了一 些用于高效生成多种概率分布的样本值的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.11775894,  0.76466835, -2.55278463,  0.06391239],\n",
       "       [-0.67175838,  0.74302606,  0.24818028,  2.34022025],\n",
       "       [-1.22458484, -0.4639177 ,  0.50069112,  1.62663704],\n",
       "       [ 0.53273788, -0.68709111,  0.25458657,  0.75635203]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# normal来得到一个标准正态分布的4×4样本数组\n",
    "samples = np.random.normal(size=(4, 4))\n",
    "samples"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 而Python内置的random模块则只能一次生成一个样本值。从下面 的测试结果中可以看出，如果需要产生大量样本值，numpy.random快 了不止一个数量级"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from random import normalvariate\n",
    "N = 1000000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "708 ms ± 5.61 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "%timeit samples = [normalvariate(0, 1) for _ in range(N)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "22.5 ms ± 431 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit np.random.normal(size=N)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 部分numpy.random函数\n",
    "|函数|说明|\n",
    "|:-|:-|\n",
    "|seed|确定随机数生成器种子|\n",
    "|permutation|返回一个序列的随机排列或返回一个随机排列的范围|\n",
    "|shuffle|对一个序列就地随机排列|\n",
    "|rand|产生均匀分布的样本值|\n",
    "|randint|从给定的上下限范围内随机选取整数|\n",
    "|randn|产生正态分布(平均值为0，标准差为1)的样本值，类似于MATLAB接口|\n",
    "|binomial|产生二项分布的样本值|\n",
    "|normal|产生正态(高斯)分布的样本值|\n",
    "|beta|产生Beta分布的样本值|\n",
    "|chisquare|产生卡方分布的样本值|\n",
    "|gamma|产生Gamma分布的样本值|\n",
    "|uniform|产生在[0,1)中均匀分布的样本值|"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "3207008acf26a07c58f9163727980777c0d92fa298586eda8a314250b7931cb4"
  },
  "kernelspec": {
   "display_name": "Python 3.7.10 ('pybd')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
